import pandas as pd
import numpy as np

parties_issue_attacker = True


party_dict_BE =  {52: ["N-VA", "CD&V", "sp.a", "Open-Vld", "Groen", "VB"], 53: ["N-VA", "CD&V", "sp.a", "Open-Vld", "Groen", "VB"],54.1:["N-VA", "CD&V", "sp.a", "Open-Vld", "Groen", "VB"], 54.2:["N-VA", "CD&V", "sp.a", "Open-Vld", "Groen", "VB"], 55: ["N-VA", "CD&V", "sp.a", "Open-Vld", "Groen", "VB"]}
party_dict_UK =  {54: ["Con", "Lab"], 55:["Con", "Lab"], 56:["Con", "Lab"], 57:["Con", "Lab"], 58:["Con", "Lab"]}

party_dict = {1: party_dict_BE, 3:party_dict_UK}

issues =[1,2,3,4,5,6,7,8,9,10,12,13,14,15,16,17,18,19,20,21,23]

if parties_issue_attacker:
    xl = pd.read_excel(r'C:\Users\Zeljko\Desktop\Political Communication - Replication Materials\attack_data.xlsx') # Note: Change working directory
    result = pd.DataFrame()

    for country in [1, 3]:

        xl1 = xl.loc[xl["con"]==country]
        xl1 = xl1.loc[xl1["appeal"]==1]
        xl1= xl1[["date", "legislature_robust", "qt", "year", "appeal", "issue", "party_subject"]]
        days= xl1["date"].unique()


        for day in days:
            print(country, day)
            if day in xl1["date"].values:
                df= xl1.loc[xl1["date"] == day]
                leg = df["legislature_robust"].iloc[0]

                for subject in party_dict[country][leg]:
                    df_sub = df.loc[df["party_subject"]== subject]
                    for issue in issues:
                        if df_sub.empty:
                            number = 0

                        else:
                            df_obj = df_sub.loc[df_sub["issue"]==issue]
                            number = len(df_obj)



                        df_add = pd.DataFrame([country,leg, day, subject, issue, number,], index=["con","legislature","date", "party", "issue", "n_attacks" ,])
                        result = pd.concat([result,df_add.T])

    result.to_excel(r'C:\Users\Zeljko\Desktop\Political Communication - Replication Materials\final_data.xlsx') # Note: Change working directory

